package com.youmans.userservice.aop;

import com.youmans.common_utils.config.Result;
import com.youmans.common_utils.handler.JwtUtils;
import com.youmans.userservice.entity.YoumansLoginLog;
import com.youmans.userservice.mapper.YoumansLoginLogMapper;
import com.youmans.userservice.service.YoumansLoginLogService;
import com.youmans.userservice.website.IpUtils;
import com.youmans.userservice.website.WebsiteApplication;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;




@Component
@Aspect
public class LoggerAspect {



    @Autowired
    YoumansLoginLogService youmansLoginLogService ;
//    @Autowired
//    YoumansLoginLogMapper youmansLoginLogMapper;

    @Before("execution(* com.youmans.userservice.controller.MyController.*(..))")
    public void before(JoinPoint joinPoint){
        //前置通知
        String name = joinPoint.getSignature().getName();
        System.out.println(name+"方法的参数是："+ Arrays.toString(joinPoint.getArgs()));


    }
    @AfterReturning(value = "execution(* com.youmans.userservice.controller.MyController.*(..))",returning = "result")
    public void afterReturning(JoinPoint joinPoint, Result result){
        Signature signature = joinPoint.getSignature() ;
        String name = signature.getName();
        System.out.println(name+"方法的结果是："+ result);



        RequestAttributes repa = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) repa;
        HttpServletRequest request = sra.getRequest();


        String requestURI = request.getRequestURI();
        String methodname = request.getMethod();
        String ip = IpUtils.getIpAddr(request);
        String token = (String) request.getAttribute("token");
        String userId = JwtUtils.getMemberIdByToken(token);
        String userName = JwtUtils.getUsernameByJwtToken(token);

//            userName ;
        String address = WebsiteApplication.getAddressByIp(ip);
        YoumansLoginLog youmansLoginLog =  new YoumansLoginLog();
        youmansLoginLog.setAddress(address) ;
        youmansLoginLog.setMethod(methodname);
        youmansLoginLog.setIp(ip) ;
        youmansLoginLog.setModule(requestURI) ;
        youmansLoginLog.setUserid(Integer.parseInt(userId)) ;
//        youmansLoginLog.setId(1l);
        youmansLoginLog.setCreateDate(new Date());
        youmansLoginLog.setUsername(userName);

//        youmansLoginLogMapper.insert(youmansLoginLog);
        youmansLoginLogService.save(youmansLoginLog);

    }

}
